package com.wtz.temptest.vertx;

import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.jdbc.JDBCClient;

public class DemoOne {
    private final static String MYSQL_DRIVER = "com.mysql.jdbc.Driver";

    public static void main(String[] args) {
        JsonObject mysqlClientConfig = new JsonObject();
        mysqlClientConfig.put("url", "jdbc:mysql://k3:3306/tiezhu")
                .put("driver_class", MYSQL_DRIVER)
                .put("max_pool_size", 5)
                .put("user", "root")
                .put("password", "admin123");

        VertxOptions vo = new VertxOptions();
        vo.setEventLoopPoolSize(1);
        vo.setWorkerPoolSize(5);
        Vertx vertx = Vertx.vertx(vo);
        JDBCClient client = JDBCClient.createShared(vertx, mysqlClientConfig);
        client.getConnection(connect -> {
            if (connect.failed()) {
                throw new IllegalArgumentException(connect.cause());
            }

            if (connect.succeeded()) {
                System.out.println("Get connection succeed!");
            }
        });

        client.querySingle("SELECT * FROM TestDemoOne",
                rs -> {
                    if (rs.failed()) {
                        System.out.println(rs.cause().getMessage());
                    }

                    if (rs.succeeded()) {
                        rs.result().stream().iterator().forEachRemaining(
                                System.out::println
                        );
                    }
                });
        client.close(
                done -> {
                    if (done.failed()) {
                        System.out.println("close SQL client error!");
                    } else {
                        System.out.println("close SQL client succeed!");
                    }
                }
        );
    }
}
